class IndexController < ApplicationController
  before_filter :sys_before_log

  def index
    if session[:account_id] == nil
      redirect_to '/index/login'
    end
    if session[:user_type] == 'user'
      @menus = @@menuMap[2]
    elsif session[:user_type] == 'admin'
      @menus = @@menuMap[1]
    end
    @menus = @menus.to_json
  end

  def checkLogin
    begin
      account = Account.find_last_by_username_and_password(params[:username],params[:password])
      if account == nil
        info = "登录失败，请确认用户名和密码！"
        puts 'ok'
      elsif account.user_type == 'user'
        session[:account_id] = account.id
        session[:user_type] = 'user'
        session[:user_name] = account.username
        info = "success"
      else
        session[:account_id] = account.id
        session[:user_type] = 'admin'
        session[:user_name] = account.username
        info = "success"
      end
      result = "{success:true,info:'#{info}'}"
      render :text=>result, :layout=>false
    rescue Exception => e
      puts "----------------------Exception--------------------------"
      puts e
    end
  end


  #退出系统
  def logout
    reset_session
    info="退出成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end


  private

  @@menuMap = {
    1 => [
      { :id => 1, :name => '列车检修', :image => '/images/plugin.gif',
        :url => '', :qtip => '列车检修', :leaf => false,
        :children => [
          # --- 检修监控 ---
          { :id => 10, :name => '检修任务管理', :image => '/images/plugin.gif',
            :url => 'traintask/trainTaskList', :qtip => '检修任务管理', :leaf => true
          },
          { :id => 11, :name => '检修日报管理', :image => '/images/plugin.gif',
            :url => 'daily/dailyList', :qtip => '检修日报管理', :leaf => true
          },
          { :id => 12, :name => '员工打卡记录', :image => '/images/plugin.gif',
            :url => 'record/recordList', :qtip => '员工打卡记录', :leaf => true
          }
        ]
      },
      { :id => 2, :name => '后台管理', :image => '/images/plugin.gif',
        :url => '', :qtip => '后台管理', :leaf => false,
        :children => [
          # --- 检修监控 ---
          { :id => 20, :name => '列车管理', :image => '/images/plugin.gif',
            :url => 'train/trainList', :qtip => '列车管理', :leaf => true
          },
          #          { :id => 21, :name => '车次管理', :image => '/images/plugin.gif',
          #            :url => 'number/numberList', :qtip => '车次管理', :leaf => true
          #          },
          { :id => 22, :name => '交路管理', :image => '/images/plugin.gif',
            :url => 'router/routerList', :qtip => '车次管理', :leaf => true
          },
          { :id => 23, :name => '组队管理', :image => '/images/plugin.gif',
            :url => 'team/teamList', :qtip => '组队管理', :leaf => true
          },
          { :id => 27, :name => '员工管理', :image => '/images/plugin.gif',
            :url => 'worker/workerList', :qtip => '员工管理', :leaf => true
          },
          { :id => 28, :name => '股道管理', :image => '/images/plugin.gif',
            :url => 'track/trackList', :qtip => '股道管理', :leaf => true
          },
          { :id => 24, :name => '养护任务管理', :image => '/images/plugin.gif',
            :url => 'work/workList', :qtip => '养护任务管理', :leaf => true
          },
          { :id => 25, :name => '模板管理', :image => '/images/plugin.gif',
            :url => 'template/templateList', :qtip => '模板管理', :leaf => true
          },
          { :id => 26, :name => '账号管理', :image => '/images/plugin.gif',
            :url => 'account/accountList', :qtip => '账号管理', :leaf => true
          },
          { :id => 29, :name => '系统日志', :image => '/images/plugin.gif',
            :url => 'log/logList', :qtip => '系统日志', :leaf => true
          }
        ]
      }
    ],
    2 => [
      { :id => 1, :name => '列车检修', :image => '/images/plugin.gif',
        :url => '', :qtip => '列车检修', :leaf => false,
        :children => [
          # --- 检修监控 ---
          { :id => 10, :name => '检修任务管理', :image => '/images/plugin.gif',
            :url => 'traintask/trainTaskList', :qtip => '检修任务管理', :leaf => true
          },
          { :id => 11, :name => '检修日报管理', :image => '/images/plugin.gif',
            :url => 'daily/dailyList', :qtip => '检修日报管理', :leaf => true
          },
          { :id => 12, :name => '员工打卡记录', :image => '/images/plugin.gif',
            :url => 'record/recordList', :qtip => '员工打卡记录', :leaf => true
          }
        ]
      }
    ]
  }

end
